package com.lp.multi.juc.collection.unsafe;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;

/**
 * @author: lp
 * @create: 2021-06-27 14:55
 * @description: ArrayList线程不安全测试
 * 搞懂并发不安全原理需要读ArrayList源码
 * 让线程安全方案：
 * 1.Vector
 * 2.Collections.synchronizedList()转化我线程安全集合
 * 3.并发安全集合CopyOnWriteArrayList
 */
//java.util.ConcurrentModificationException  并发修改异常
public class ArrayListTest {
    public static void main(String[] args) throws InterruptedException {
        List<String> arrayList = new ArrayList();
        for (int i = 0; i < 1000; i++) {
            final int  temp = i;
            //（）->{j}表达式中j必须是final类型
            new Thread(()->{
                arrayList.add(temp+"");
                //如果去掉打印，就不会报错：java.util.ConcurrentModificationException
                System.out.println(arrayList);
            },String.valueOf(i)).start();
        }

        TimeUnit.SECONDS.sleep(10);
        System.out.println(arrayList.size());
    }
}

/**
 * 使用Vector
 */
class SafeVecotrTest {
    public static void main(String[] args) throws InterruptedException {
        Vector<String> arrayList = new Vector();
        for (int i = 0; i < 1000; i++) {
            final int  temp = i;
            //（）->{j}表达式中j必须是final类型
            new Thread(()->{
                arrayList.add(temp+"");
                //如果去掉打印，就不会报错：java.util.ConcurrentModificationException
                System.out.println(arrayList);
            },String.valueOf(i)).start();
        }

        TimeUnit.SECONDS.sleep(10);
        System.out.println(arrayList.size());
    }
}

/**
 * 使用Collections转化成安全集合
 */
class SafeCollectionsTest {
    public static void main(String[] args) throws InterruptedException {
        List<String> arrayList = Collections.synchronizedList(new ArrayList<String>());
        for (int i = 0; i < 1000; i++) {
            final int  temp = i;
            //（）->{j}表达式中j必须是final类型
            new Thread(()->{
                arrayList.add(temp+"");
                //如果去掉打印，就不会报错：java.util.ConcurrentModificationException
                System.out.println(arrayList);
            },String.valueOf(i)).start();
        }

        TimeUnit.SECONDS.sleep(10);
        System.out.println(arrayList.size());
    }
}

/**
 * 使用CopyOnWriteArrayList
 * 1.CopyOnWriteArrayList比Vector好在哪里？
 * 2.CopyOnWriteArrayList实现线程安全原理?
 */
class SafeCopyOnWriteArrayListTest {
    public static void main(String[] args) throws InterruptedException {
        List<String> arrayList = new CopyOnWriteArrayList<String>();
        for (int i = 0; i < 1000; i++) {
            final int  temp = i;
            //（）->{j}表达式中j必须是final类型
            new Thread(()->{
                arrayList.add(temp+"");
                //如果去掉打印，就不会报错：java.util.ConcurrentModificationException
                System.out.println(arrayList);
            },String.valueOf(i)).start();
        }

        TimeUnit.SECONDS.sleep(10);
        System.out.println(arrayList.size());
    }
}

